package com.faner.fast.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.faner.fast.annotation.SysLog;
import com.faner.fast.base.PageDTO;
import com.faner.fast.constant.CacheConstants;
import com.faner.fast.upms.model.SysDictDO;
import com.faner.fast.upms.model.SysDictItemDO;
import com.faner.fast.upms.service.SysDictItemService;
import com.faner.fast.upms.service.SysDictService;
import com.faner.fast.util.PageUtil;
import com.faner.fast.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
@Api(value = "dict", tags = "系统-字典管理模块")
@ApiSupport(order = 40)
public class DictController {

    private final SysDictItemService sysDictItemService;

    private final SysDictService sysDictService;

    /**
     * 分页查询字典信息
     * @param page 分页对象
     * @param sysDict 字典信息
     * @return 分页对象
     */
    @ApiOperation(value = "字典表-分页列表")
    @ApiOperationSupport(order = 10,author = "faner")
    @GetMapping("/dict/page")
    public R<PageUtil<SysDictDO>> getDictPage(@Validated PageDTO page, SysDictDO sysDict) {
        Page<SysDictDO> sysDictPage = sysDictService.getSysDictPage(page, sysDict);
        return R.ok(PageUtil.resetPage(sysDictPage));
    }

    /**
     * 通过ID查询字典信息
     * @param id ID
     * @return 字典信息
     */
    @ApiOperation(value = "字典表-详情")
    @ApiOperationSupport(order = 20,author = "faner")
    @GetMapping("/dict/detail")
    public R getDictById(@RequestParam Long id) {
        return R.ok(sysDictService.getById(id));
    }



    /**
     * 通过字典类型查找字典
     * @param type 类型
     * @return 同类型字典
     */
    @ApiOperation(value = "字典表-字典类型查找字典")
    @ApiOperationSupport(order = 30,author = "faner")
    @GetMapping("/dict/type")
    @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type")
    public R getDictByType(@RequestParam String type) {
        return R.ok(sysDictItemService.list(Wrappers.<SysDictItemDO>query().lambda().eq(SysDictItemDO::getType, type)));
    }

    /**
     * 添加字典
     * @param sysDict 字典信息
     * @return success、false
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_add')")
    @SysLog("添加字典")
    @ApiOperation(value = "字典表-添加字典")
    @ApiOperationSupport(order = 40,author = "faner")
    @PostMapping("/dict/save")
    public R save(@Valid @RequestBody SysDictDO sysDict) {
        sysDictService.save(sysDict);
        return R.ok();
    }

    /**
     * 删除字典，并且清除字典缓存
     * @param id ID
     * @return R
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_del')")
    @SysLog("删除字典")
    @ApiOperation(value = "字典表-删除字典")
    @ApiOperationSupport(order = 50,author = "faner")
    @PostMapping("/dict/delete")
    public R removeById(@RequestParam Long id) {
        sysDictService.removeDict(id);
        return R.ok();
    }

    /**
     * 修改字典
     * @param sysDict 字典信息
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_edit')")
    @SysLog("修改字典")
    @ApiOperation(value = "字典表-修改字典")
    @ApiOperationSupport(order = 60,author = "faner")
    @PostMapping("/dict/update")
    public R updateById(@Valid @RequestBody SysDictDO sysDict) {
        sysDictService.updateDict(sysDict);
        return R.ok();
    }

    /**
     * 分页查询字典项
     * @param page 分页对象
     * @param sysDictItem 字典项
     * @return
     */
    @ApiOperation(value = "字典项-分页查询")
    @ApiOperationSupport(order = 70,author = "faner")
    @GetMapping("/dict/item/page")
    public R getSysDictItemPage(@Validated PageDTO page, SysDictItemDO sysDictItem) {
        Page<SysDictItemDO> sysDictItemPage = sysDictItemService.getSysDictItemPage(page, sysDictItem);
        return R.ok(PageUtil.resetPage(sysDictItemPage));
    }

    /**
     * 通过id查询字典项
     * @param id id
     * @return R
     */
    @ApiOperation(value = "字典项-通过id查询字典项")
    @ApiOperationSupport(order = 80,author = "faner")
    @GetMapping("/dict/item/detail")
    public R getDictItemById(@RequestParam("id") Long id) {
        return R.ok(sysDictItemService.getById(id));
    }

    /**
     * 新增字典项
     * @param sysDictItem 字典项
     * @return R
     */
    @SysLog("新增字典项")
    @ApiOperation(value = "字典项-新增字典项")
    @ApiOperationSupport(order = 90,author = "faner")
    @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
    @PostMapping("/dict/item/save")
    public R save(@RequestBody SysDictItemDO sysDictItem) {
        return R.ok(sysDictItemService.save(sysDictItem));
    }

    /**
     * 修改字典项
     * @param sysDictItem 字典项
     * @return R
     */
    @SysLog("修改字典项")
    @ApiOperation(value = "字典项-修改字典项")
    @ApiOperationSupport(order = 100,author = "faner")
    @PostMapping("/dict/item/update")
    public R updateById(@RequestBody SysDictItemDO sysDictItem) {
        return R.ok(sysDictItemService.updateDictItem(sysDictItem));
    }
    
    /**
     * 通过id删除字典项
     * @param id id
     * @return R
     */
    @SysLog("删除字典项")
    @ApiOperation(value = "字典项-通过id删除字典项")
    @ApiOperationSupport(order = 110,author = "faner")
    @PostMapping("/dict/item/delete")
    public R removeDictItemById(@RequestParam Long id) {
        return R.ok(sysDictItemService.removeDictItem(id));
    }

}
